/**
 * 
 * SurfaceGeometry is a class that is used to create geometries based
 * using the sweep surface (surface of revolution) technique.
 *
 * fun is used to determine x,y,z given the u,v values. Slices(u) and stacks(v)
 * represent the number of points in the profile curve and sweep steps respectively.
 * 
 */
THREE.SurfaceGeometry = function(slices, stacks, fun) { 
    THREE.Geometry.call( this );
    
    var pi = Math.PI;
    var x, y, z;
    
    var i, il, theta, j, phi, vrtx;
    
    // prepare vertex positions (x,y,z)
    for (i=0; i<=slices; i++) {
      theta = i * 2 * pi / slices;    
      for (j=0;j<stacks;j++) {
        phi = j * 2 * pi / stacks;
        vrtx = fun(theta, phi);
        this.vertices.push( vrtx );
      }
    }
    
    var v = 0, next;
    // setup faces as vertex indices - the three corners of each triangle
    for (i=0;i<slices;i++) {
      for (j=0;j<stacks;j++) {
        next = (j + 1) % stacks;
        this.faces.push( new THREE.Face3( v + j, v + next, v + j + stacks ) );
        this.faces.push( new THREE.Face3( v + next, v + next + stacks, v + j + stacks ) );      
      }
      v += stacks;
    }
    
    // compute centroids for all faces
    this.computeCentroids();

    // compute normals for all faces
    this.computeFaceNormals();

    // compute vertex normals by averaging face normals
    this.computeVertexNormals();
};

// SurfaceGeometry extends the base class Geometry
THREE.SurfaceGeometry.prototype = new THREE.Geometry();
THREE.SurfaceGeometry.prototype.constructor = THREE.SurfaceGeometry;